from flask import Blueprint, render_template
from flask_login import login_required, current_user
from sqlalchemy import and_

from applications.exts.systeminformation import webinformastion
from applications.models import WxUser, Orders, Goods, Classify

admin_bp = Blueprint('admin', __name__, url_prefix='/admin')


# 首页
@admin_bp.route('/')
@login_required
def index():
    user = current_user
    return render_template('admin/index.html', user=user)


# 控制台页面
@admin_bp.route('/welcome')
@login_required
def welcome():
    wxuser = WxUser.query.all()
    good = Goods.query.all()

    # 获取
    ords = Orders.query.filter().all()
    datax = []
    for item in ords:
        dataitem = item.redatetime.strftime("%Y-%m-%d")
        datax.append(dataitem)
    # data = list(set(datax))

    data = list(set(datax))
    data.sort(key=datax.index)
    # new_li = list(set(datax))
    # datax.sort(key=new_li.index)
    print('datax', data)
    data_sum = []
    # sum = Orders.query.filter.all()
    temp = {}
    for i in data:
        count = 0
        for item in ords:
            temp = item.redatetime.strftime("%Y-%m-%d")
            if temp == i:
                count += 1
        # datax.append(count)
        data_sum.append(count)


    print('data_sum', data_sum)
    order = Orders.query.filter(Orders.order_status == 1).all()
    # 商品
    xgname = []
    ysale = []
    for iten in good:
        gname = iten.gname
        sale = iten.sales
        xgname.append(gname)
        ysale.append(sale)
    #     订单
    orderlist = []
    auccess = 0  # 已完成
    count = 0  # 已取消
    outgood = 0  # 已出货
    orders = Orders.query.all()

    for item in orders:
        if item.order_status == 1 and item.pay_status == 1:
            auccess += 1
        if item.order_status == 0 and item.pay_status == 1:
            outgood += 1
        if item.order_status == 2:
            count += 1
    auuess = {'value': auccess, 'name': '已完成'}
    count = {'value': count, 'name': '已取消'}
    outgood = {'value': outgood, 'name': '已出货'}
    orderlist.append(auuess)
    orderlist.append(count)
    orderlist.append(outgood)
    print(orderlist)

    # 商品分类
    good_classify = Classify.query.all()
    goodclassify = []
    for item in good_classify:
        temp = {}
        count = Goods.query.filter(Goods.classifyid == item.classifyid).count()
        temp['value'] = count
        temp['name'] = item.gname
        goodclassify.append(temp)

    # 销售额
    money_sum = 0.0
    moneys_order = Orders.query.filter(and_(Orders.order_status == 1, Orders.pay_status == 1)).all()
    for i in moneys_order:
        money_sum += i.order_price

    # 获取系统信息
    webinformation = webinformastion()
    return render_template('admin/console/console.html', money_sum=money_sum, wxuser_num=len(wxuser), good_num=len(good),
                           order_num=len(order), goods={'xgname': xgname, 'ysale': ysale},
                           orderlist=orderlist,
                           orderlist_count=len(orderlist)
                           , goodclassify=goodclassify,
                           # 随机颜色
                           goodclassify_count=len(goodclassify), webinformation=webinformation,
                           data_orders={'datax': data, 'data_sum': data_sum})
